/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package im.eventservice;

import im.clusterhead.ClusterHeadInfo;
import im.general.AbstractListenBehaviour;
import im.general.Config;
import jade.lang.acl.ACLMessage;
import jade.lang.acl.MessageTemplate;
import jade.util.Logger;
import java.util.logging.Level;

/**
 *
 * @author Gnaskky
 */
public class NewCHListernerBehaviour extends AbstractListenBehaviour {

    private EventServiceAgent es;

    NewCHListernerBehaviour(EventServiceAgent es) throws Exception {
        this.es = es;
        ACLMessage wrap_msg = new ACLMessage( ACLMessage.PROPOSE );
        wrap_msg.addUserDefinedParameter( Config.IM_MSG_TYPE, Config.NEW_CH_NOTIFY_MSG);
        this.mt = MessageTemplate.MatchCustom(wrap_msg, true );
    }

    @Override
    protected void handleMessage(ACLMessage in_msg) {
        try {

            String ch_name = in_msg.getUserDefinedParameter(Config.NEW_CH_NAME);

            if( Config.TRACE_LVL > Config.ULTRA_TR_LVL )
                System.out.println("[NewCHListernerBehav]\tNEW_CH_NOTIFY_MSG\t\t" + ch_name);

            es.addHead(ch_name,(ClusterHeadInfo) in_msg.getContentObject());

        } catch (Exception ex) {
            Logger.getLogger(ClusterJoinNotifyBehaviour.class.getName()).log(Level.SEVERE, null, ex);
            ex.printStackTrace();
        }
    }
    
}